package com.aide.simplification.popedom.taglib;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.Input;
import org.apache.ecs.html.Option;
import org.apache.ecs.html.Script;
import org.apache.ecs.html.Select;

import com.aide.simplification.global.Utils;
import com.aide.simplification.popedom.popedom.Dept;
import com.aide.simplification.popedom.popedom.User;
import com.aide.simplification.popedom.popedom.base.PopedomOP;

public class UserComboxTag extends TagSupport {
	private static final long serialVersionUID = 1938473774665824799L;
	private String name = "";
	private String id = "";
	private String style = "";
	private boolean multiple = false;
	private String attributes = "";
	private String opsign = ">=";
	private int level = 0;
	private String pop = "0";
	private String defaultValue = "";
	private boolean show = false;

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	private String nopop = "0";

	private String deptId = "";
	private String deptName = "";
	private String deptValue = "";
	private String deptAttributes = "";

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
//		ApplicationContext ctx = WebApplicationContextUtils
//				.getWebApplicationContext(pageContext.getServletContext());
//		User user = (User) ctx.getBean("User");
//		Dept dept = (Dept) ctx.getBean("Dept");
		User user = new User();
		Dept dept = new Dept();
		if (show) {
			Map<String, Object> map = user.findByPk(defaultValue);
			if(map!=null){
				try {
					out.println(map.get("name").toString());
				} catch (IOException e) {
				}
			}
			if(!deptValue.equals("")){
				Map<String, Object> deptmap = dept.findByPk(deptValue);
				if(deptmap!=null){
					try {
						out.println("&nbsp;&nbsp;"+deptmap.get("dept").toString());
					} catch (IOException e) {
					}
				}
			}
		} else {
			Input input = new Input();
			input.setType("hidden");
			input.setName(name);
			input.setID(name);
			input.setValue("");

			Select select = new Select();
			if (!"".equals(style)) {
				select.setStyle(style);
			}
			select.setName(id);
			select.setID(id);
			select.setMultiple(multiple);

			if (!attributes.equals("")) {
				Pattern pattern = Pattern.compile("\\s+");
				String[] attribute = pattern.split(attributes);
				for (int i = 0; i < attribute.length; i++) {
					String[] temp = attribute[i].split("=");
					if (temp != null && temp.length == 2) {
						select.addAttribute(temp[0], temp[1]);
					} else if (temp.length == 1) {
						select.addAttribute(temp[0], "");
					}
				}
			}
			PopedomOP popop = new PopedomOP();
			List<Map<String, Object>> list = user.findByLevel(level, opsign);
			Option option1 = new Option("请选择", "", "请选择");
			select.addElement(option1);
			for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
					.hasNext();) {
				Map<String, Object> map = iterator.next();
				if ((pop.equals("0") || popop.checkPopAnd(pop, (String) map
						.get("pop")))
						&& !map.get("useruuid").toString().equals(
								Utils.getConfig("pop.admin"))) {
					Option option = new Option(map.get("name").toString(), map
							.get("useruuid").toString(), map.get("name")
							.toString());
					if (map.get("useruuid").toString()
							.equals(this.defaultValue)) {
						option.setSelected(true);
						input.setValue(map.get("name").toString());
					}
					select.addElement(option);
				}
			}
			if (!"".equals(deptId)) {
				select.setOnChange("$j('#" + name + "').val($j('#" + id
						+ " option:selected').text());getDeptSubSelect('"
						+ deptName + "','" + deptId + "',this.value);");
			} else {
				select.setOnChange("$j('#" + name + "').val($j('#" + id
						+ " option:selected').text())");
			}
			Script script = new Script();
			script.setType("text/javascript");
			script.setTagText("var $j;if(!$j){$j = jQuery.noConflict()};");
			try {
				out.println(script.toString());
				out.println(input.toString() + select.toString());
				if (!"".equals(deptId)) {
					Select selectDept = new Select();
					selectDept.setName(deptId);
					selectDept.setID(deptId);
					selectDept.setOnChange("$j('#" + deptName + "').val($j('#"
							+ deptId + " option:selected').text());");

					Input inputDept = new Input();
					inputDept.setType("hidden");
					inputDept.setName(deptName);
					inputDept.setID(deptName);
					inputDept.setValue("");

					if (!deptAttributes.equals("")) {
						Pattern pattern = Pattern.compile("\\s+");
						String[] attribute = pattern.split(deptAttributes);
						for (int i = 0; i < attribute.length; i++) {
							String[] temp = attribute[i].split("=");
							if (temp != null && temp.length == 2) {
								selectDept.addAttribute(temp[0], temp[1]);
							} else if (temp.length == 1) {
								selectDept.addAttribute(temp[0], "");
							}
						}
					}
					Option optionDept1 = new Option("请选择", "", "请选择");
					if (!"".equals(defaultValue)) {
						List<Map<String, Object>> listDept = user
								.findByUserUUIDIsUseInfo(defaultValue);
						if (listDept.size() != 1) {
							selectDept.addElement(optionDept1);
						} else {
							inputDept.setValue(listDept.get(0).get("dept")
									.toString());
						}
						for (Map<String, Object> mapDept : listDept) {
							Option optionDept = new Option(mapDept.get("dept")
									.toString(), mapDept.get("deptid")
									.toString(), mapDept.get("dept").toString());
							if (mapDept.get("deptid").toString().equals(
									deptValue)) {
								optionDept.setSelected(true);
								inputDept.setValue(mapDept.get("dept")
										.toString());
							}
							selectDept.addElement(optionDept);
						}
					} else {
						selectDept.addElement(optionDept1);
					}

					out.println(inputDept.toString() + selectDept.toString());
				}
			} catch (IOException e) {
			}
		}
		return EVAL_PAGE;
	}

	public String getDeptAttributes() {
		return deptAttributes;
	}

	public void setDeptAttributes(String deptAttributes) {
		this.deptAttributes = deptAttributes;
	}

	public String getPop() {
		return pop;
	}

	public void setPop(String pop) {
		this.pop = pop;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getAttributes() {
		return attributes;
	}

	public void setAttributes(String attributes) {
		this.attributes = attributes.trim();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}

	public String getNopop() {
		return nopop;
	}

	public void setNopop(String nopop) {
		this.nopop = nopop;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getDeptValue() {
		return deptValue;
	}

	public void setDeptValue(String deptValue) {
		this.deptValue = deptValue;
	}

	public String getOpsign() {
		return opsign;
	}

	public void setOpsign(String opsign) {
		this.opsign = opsign;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

}
